package com.djakapm.swing.component;


import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import com.djakapm.swing.common.ContainerFactory;

import javax.swing.*;
import java.awt.*;

public class SpecForContainerFactory {


    @Test
    public void testNewGridLayoutedPanel() {
        JPanel panel = ContainerFactory.newGridLayoutedPanel();
        assertThat("Should create new GridLayout-ed panel with default setting",panel,notNullValue());
        assertTrue("Panel has GridLayout as its LayoutManager ",(panel.getLayout() instanceof GridLayout));
    }

    @Test
    public void testNewGridLayoutedPanelWithRowsAndColumns() {
        JPanel panel = ContainerFactory.newGridLayoutedPanel(2,3);
        assertThat("Should create new GridLayout-ed panel with default setting",panel,notNullValue());
        assertTrue("Panel has GridLayout as its LayoutManager ",(panel.getLayout() instanceof GridLayout));
        GridLayout layout = (GridLayout) panel.getLayout();
        assertTrue("Number of rows equal to 2",layout.getRows() == 2);
        assertTrue("Number of columns equal to 3",layout.getColumns() == 3);
    }

    @Test
    public void testNewGridLayoutedPanelWithRowsAndColumnsAndHGapAndVGap() {
        JPanel panel = ContainerFactory.newGridLayoutedPanel(2,3,5,4);
        assertThat("Should create new GridLayout-ed panel with default setting",panel,notNullValue());
        assertTrue("Panel has GridLayout as its LayoutManager ",(panel.getLayout() instanceof GridLayout));
        GridLayout layout = (GridLayout) panel.getLayout();
        assertTrue("Number of rows equal to 2",layout.getRows() == 2);
        assertTrue("Number of columns equal to 3",layout.getColumns() == 3);
        assertTrue("Horizontal gap equal to 5",layout.getHgap() == 5);
        assertTrue("Vertical gap equal to 4",layout.getVgap() == 4);
    }


	@Test
	public void testNewFrame() {
        JFrame frame = ContainerFactory.newFrame();
		assertThat("should create new JFrame with default dimension and no title",frame,notNullValue());
        assertTrue("Frame title must be empty","".equals(frame.getTitle()));
        assertTrue("Should has width equal to default width",frame.getWidth() == ContainerFactory.FRAME_DEFAULT_WIDTH);
        assertTrue("Should has height equal to default height",frame.getHeight() == ContainerFactory.FRAME_DEFAULT_HEIGHT);
        assertTrue("Should has height equal to default height",frame.getHeight() == ContainerFactory.FRAME_DEFAULT_HEIGHT);
	}

	@Test
	public void testNewFrameString() {
        JFrame frame = ContainerFactory.newFrame("Title Test");

		assertThat("Should create new JFrame with default dimension and user defined title",frame,notNullValue());
        assertTrue("Frame title must be empty","Title Test".equals(frame.getTitle()));
        assertTrue("Should has width equal to default width",frame.getWidth() == ContainerFactory.FRAME_DEFAULT_WIDTH);
        assertTrue("Should has height equal to default height",frame.getHeight() == ContainerFactory.FRAME_DEFAULT_HEIGHT);
	}

	@Test
	public void testNewFrameIntInt() {
        JFrame frame = ContainerFactory.newFrame(800,600);
		assertThat("Should create new JFrame with user defined dimension and no title",frame,notNullValue());
        assertTrue("Frame title must be empty","".equals(frame.getTitle()));
        assertTrue("Should has width equal to 800 pixel",frame.getWidth() == 800);
        assertTrue("Should has height equal to 600 pixel",frame.getHeight() == 600);
	}

	@Test
	public void testNewFrameStringIntInt() {
        JFrame frame = ContainerFactory.newFrame("Title Test with dimension",1024,768);
		assertThat("Should create new JFrame with user defined dimension and title",frame,notNullValue());
        assertTrue("Should has title","Title Test with dimension".equals(frame.getTitle()));
        assertTrue("Should has width equal to 1024 pixel",frame.getWidth() == 1024);
        assertTrue("Should has height equal to 768 pixel",frame.getHeight() == 768);
	}

	@Test
	public void testNewPanel() {
		assertThat("Should create new JPanel with default setting",ContainerFactory.newPanel(),	notNullValue());
	}

	@Test
	public void testNewBorderLayoutedPanel() {
		assertThat("Should create new BorderPanel with default setting",ContainerFactory.newBorderLayoutedPanel(),notNullValue());
	}

	@Test
	public void testNewFlowLayoutedPanel() {
		assertThat("Should create new FlowPanel with default setting",ContainerFactory.newFlowLayoutedPanel(),notNullValue());
	}

	@Test
	public void testNewFlowLeftLayoutedPanel() {
        JPanel panel = ContainerFactory.newFlowLeftLayoutedPanel();
		assertThat("Should create new FlowPanel with left alignment",panel,notNullValue());
        assertTrue("Panel has FlowLayout as its LayoutManager ",(panel.getLayout() instanceof FlowLayout));
        FlowLayout layoutManager = (FlowLayout) panel.getLayout();
        assertTrue("FlowPanel has LEFT alignment ",layoutManager.getAlignment() == FlowLayout.LEFT);
	}

	@Test
	public void testNewFlowRightLayoutedPanel() {
        JPanel panel = ContainerFactory.newFlowRightLayoutedPanel();
		assertThat("Should create new FlowPanel with right alignment",panel,notNullValue());
        assertTrue("Panel has FlowLayout as its LayoutManager ",(panel.getLayout() instanceof FlowLayout));
        FlowLayout layoutManager = (FlowLayout) panel.getLayout();
        assertTrue("FlowPanel has RIGHT alignment ",layoutManager.getAlignment() == FlowLayout.RIGHT);
	}

	@Test
	public void testNewFlowLeadingLayoutedPanel() {
        JPanel panel = ContainerFactory.newFlowLeadingLayoutedPanel();
		assertThat("Should create new FlowPanel with leading alignment",panel,notNullValue());
        assertTrue("Panel has FlowLayout as its LayoutManager ",(panel.getLayout() instanceof FlowLayout));
        FlowLayout layoutManager = (FlowLayout) panel.getLayout();
        assertTrue("FlowPanel has LEADING alignment ",layoutManager.getAlignment() == FlowLayout.LEADING);
	}

	@Test
	public void testNewFlowCenterLayoutedPanel() {
        JPanel panel = ContainerFactory.newFlowCenterLayoutedPanel();
		assertThat("Should create new FlowPanel with center alignment",	panel,notNullValue());
        assertTrue("Panel has FlowLayout as its LayoutManager ",(panel.getLayout() instanceof FlowLayout));
        FlowLayout layoutManager = (FlowLayout) panel.getLayout();
        assertTrue("FlowPanel has CENTER alignment ",layoutManager.getAlignment() == FlowLayout.CENTER);
	}

	@Test
	public void testNewFlowTrailingLayoutedPanel() {
        JPanel panel = ContainerFactory.newFlowTrailingLayoutedPanel();
        
		assertThat("Should create new FlowPanel with trailing alignment",panel,notNullValue());
        assertTrue("Panel has FlowLayout as its LayoutManager ",(panel.getLayout() instanceof FlowLayout));
        FlowLayout layoutManager = (FlowLayout) panel.getLayout();
        assertTrue("FlowPanel has TRAILING alignment ",layoutManager.getAlignment() == FlowLayout.TRAILING);
	}
}
